﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblUserAccountDAO
    {
          private DBService _db = null;

          public TblUserAccountDAO(String connectionString)
          {
              _db = new DBService(connectionString);
          }
          public bool Insert_Update(TblUserAccount user, int mode)
          {
              bool flag = false;
              try
              {             
                    _db.ExeSP_NonQuery("sp_TblUserAccount_InsUp",
                      new string[] { "@ID", "@Name", "@Address", "@Email", "@Pass", "@MobilePhone", "@IsAdmin", "@Mode" },
                      new object[] { user.ID, user.Name, user.Address, user.Email, user.Pass, user.MobilePhone, user.IsAdmin, mode },
                      CommandType.StoredProcedure);

                  flag = true;
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return flag;
          }

          public TblUserAccount GetAdmin(string Email, string PassWord)
          {
              TblUserAccount user = new TblUserAccount();
              try
              {
                  DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_AdminEmail",
                     new string[] { "@Email", "@Password" },
                     new object[] { Email, PassWord },
                    CommandType.StoredProcedure);

                  foreach (DataRow dr in dt.Rows)
                  {
                      user.Email = dr["Email"].ToString();
                      user.ID = Convert.ToInt32(dr["ID"]);
                      user.Name = dr["Name"].ToString();
                      user.Pass = dr["Pass"].ToString();

                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return user;
          }

          public bool CheckExits(string mail, int mode)
          {
              bool flag = false;
              try
              {
                  DataTable dt = _db.ExeSP_DataTable("sp_CheckEmail",
                      new string[] { "@Email", "@mode" },
                      new object[] { mail, mode },
                      CommandType.StoredProcedure);
                  if (dt.Rows.Count > 0)
                  {
                      flag = true;
                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return flag;
          }
          public DataTable GetAll()
          {
              DataTable Supp = null;
              try
              {
                  Supp = _db.ExeSP_DataTable("select * from TblUserAcount", false, CommandType.Text);

              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return Supp;
          }
    }
}
